<?php
declare(strict_types = 1);
namespace app\admin\model;
use think\Config;
use think\Model;
class Goods extends Model{
    public function getAddTimeAttr($value){
        return date('Y-n-j H:i:s',$value);
    }
    public function getIfShowAttr($value){
        if (\think\facade\Config::has('view.par_goods_if_show')){
            $config = \think\facade\Config::get('view.par_goods_if_show');
            return $config[$value];
        }
    }
    public function getRecommendedAttr($value){
        if (\think\facade\Config::has('view.par_goods_Recommended')){
            $config = \think\facade\Config::get('view.par_goods_Recommended');
            return $config[$value];
        }
    }
    //查询部分
      public function searchIfShowAttr($q,$v){

        if (isset($v) &&  ''!=$v){
             $q->where('ecm_goods.if_show',strval($v));
        }
    }
    public function searchRecommendedAttr($q,$v){
        if (isset($v) && ''!=$v ) {
            $q->where('ecm_goods.recommended','=',$v);
        }
    }
    public function searchKeywordsAttr($q,$v,$data){
        if (''!==$v){
            $q->where('goods_name','like','%'.$v.'%');
        }
    }
    public function searchCateIdAttr($q,$v,$data){
        if (''!==$v){
            $q->where('ecm_goods.cate_id',$v);
        }
    }
    public function searchAddTime1Attr($q,$v,$data){
        if (!empty($v)){
            $q->whereTime('ecm_goods.add_time','<=',$v);
        }
    }
    public function searchAddTime2Attr($q,$v,$data){
        if (!empty($v)){
            $q->whereTime('ecm_goods.add_time','>=',$v);
        }
    }



    //取值部分
    public static  function getGcategory(){
        $model = self::table('ecm_gcategory')->field(true)->select();
        return $model;
    }
    public static function getGoods($data= [],$page=true,$order,$query = []){
        $field=[
            'c.cate_name','c.cate_id','g.goods_id','g.goods_name','g.cate_id','g.cate_name','g.brand','g.recommended','g.spec_qty','g.if_show',
            'g.add_time','g.last_update','g.default_image','g.price',
        ];

        //取值
        $model = self::field($field)
            ->alias('g')
            ->field('c.cate_name')
            ->join(['ecm_gcategory'=>'c'],'g.cate_id=c.cate_id','left')
            ->withSearch(array_keys($data),$data)
            ->order($order);

       //分页
        if ($page){
            $list_rows= \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows'=>  $list_rows,
                'query'    => $query,
            ]);
        }else{
            return $model->select();
        }
    }


//    =======================s
//    public static  function getGcategory(){
//        $model = self::table('ecm_gcategory')->field(true)->select();
//        return $model;
//    }
//    public static function getGoods($data= [],$page=true,$order,$query = []){
//        $field=[
//            'c.cate_name','c.cate_id','g.goods_id','g.goods_name','g.cate_id','g.cate_name','g.brand','g.recommended','g.spec_qty','g.if_show',
//            'g.add_time','g.last_update','g.default_image'
//        ];
//
//        //取值
//        $model = self::field($field)
//            ->alias('g')
//            ->field('c.cate_name')
//            ->join(['ecm_gcategory'=>'c'],'g.cate_id=c.cate_id','left')
//            ->order($order);
//
//        //查询
//        if (isset($data['recommended']) && '' !==$data['recommended']) {
//            $model->where('recommended','=',"{$data['recommended']}");
//        }
//        if($data['if_show'] && '' !==$data['if_show']){
//            $model->where('if_show','=',"{$data['if_show']}");
//        }
//        if($data['spec_qty'] && '' !==$data['spec_qty']){
//            $model->where('spec_qty','=',"{$data['spec_qty']}");
//        }
//        if ($data['cid']&& '' !==$data['cid']){
//            $model->where('c.cate_id','=',"{$data['cid']}");
//        }
//        if ($data['add_time1']!='' and $data['add_time2']!=''){
//            $model->whereBetweenTime("add_time",$data['add_time1'], $data['add_time2']);
//        }
//
//        //分页
//        if ($page){
//            $list_rows= \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
//            return $model->paginate([
//                'list_rows'=>  $list_rows,
//                'query'    => $query,
//            ]);
//        }else{
//            return $model->select();
//        }
//    }
    }
